#pragma once

#include <iostream>
#include <memory>
#include <functional>
#include <string>
#define ListenConnection 1
#define NormalConnection 2
class Reactor;
class Connection;
using hander_t = std::function<void(Connection*)>;
// 不存在sockfd的概念，全局只有连接
class Connection
{
public:
    Connection(int fd)
        : _fd(fd)
    {
    }
    ~Connection()
    {
    }
    void RegisterHander(hander_t recver, hander_t sender, hander_t exceptor)
    {
        _hander_recver = recver;
        _hander_sender = sender;
        _hander_exceptor = exceptor;
    }
    int Connectionfd()
    {
        return _fd;
    }
    void SetEvent(uint32_t event)
    {
        _event = event;
    }
    uint32_t Event()
    {
        return _event;
    }
    void SetType(int type)
    {
        _type = type;
    }
    int Type()
    {
        return _type;
    }
    void SetReactor(Reactor *R)
    {
        _R = R;
    }
    void AppendInbuffer(const std::string& str)
    {
        _in_buffer+=str;
    }
    std::string& Inbuffer()
    {
        return _in_buffer;
    }
     void AppendOutbuffer(const std::string& str)
    {
        _out_buffer+=str;
    }
    std::string& Outbuffer()
    {
        return _out_buffer;
    }
    void Discard(int n)
    {
        _out_buffer.erase(0,n);
    }
private:
    int _fd;
    uint32_t _event;
    std::string _in_buffer;  // 输入缓冲区
    std::string _out_buffer; // 输出缓冲区
    int _type;               // 连接的类型（listen，normal）
public:
    Reactor *_R;
    // 处理连接的方法集
    hander_t _hander_recver;
    hander_t _hander_sender;
    hander_t _hander_exceptor;
};